package edu.ccut.saturn.studio.server.preference;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.server.dialog.EditSaturnServerDialog;
import edu.ccut.saturn.studio.server.dialog.NewSaturnServerDialog;
import edu.ccut.saturn.studio.server.model.SaturnServer;
import edu.ccut.saturn.studio.server.model.SaturnServerModel;

public class ServerPreferencePage extends PreferencePage implements
		IWorkbenchPreferencePage {

	private Text portTxt;
	private Text textSaturnPath;
	private Text textServer;
	private Combo comboName;
	private Text textServerPath;

	public static final String SATURN_NAME = "$saturn_name"; //$NON-NLS-1$
	private Map<String, SaturnServer> saturnServers = null;

	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		saturnServers = SaturnServerModel.getSaturnServers();
	}

	@Override
	protected Control createContents(Composite parent) {

		Composite topComp = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		topComp.setLayout(gridLayout);

		final Label label_2 = new Label(topComp, SWT.NONE);
		label_2.setText(Messages
				.getString("ServerPreferencePage.SaturnServiceName")); //$NON-NLS-1$

		comboName = new Combo(topComp, SWT.READ_ONLY);
		comboName.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				refreshTextValue(comboName.getText().trim());
			}
		});

		comboName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				refreshTextValue(comboName.getText().trim());
			}
		});
		final GridData gd_comboName = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		comboName.setLayoutData(gd_comboName);

		final Label label_3 = new Label(topComp, SWT.NONE);
		final GridData gd_label_3 = new GridData();
		gd_label_3.verticalIndent = 10;
		label_3.setLayoutData(gd_label_3);
		label_3.setText(Messages.getString("ServerPreferencePage.serverType")); //$NON-NLS-1$

		textServer = new Text(topComp, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_textServer = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textServer.setLayoutData(gd_textServer);

		final Label label = new Label(topComp, SWT.NONE);
		final GridData gd_label = new GridData();
		gd_label.verticalIndent = 10;
		label.setLayoutData(gd_label);
		label.setText(Messages
				.getString("ServerPreferencePage.WebContainerPath")); //$NON-NLS-1$

		textServerPath = new Text(topComp, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_textServerPath = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_textServerPath.verticalIndent = 10;
		textServerPath.setLayoutData(gd_textServerPath);

		final Label label_1 = new Label(topComp, SWT.NONE);
		final GridData gd_label_1 = new GridData();
		gd_label_1.verticalIndent = 10;
		label_1.setLayoutData(gd_label_1);
		label_1.setText(Messages
				.getString("ServerPreferencePage.SaturnServicePosition")); //$NON-NLS-1$

		textSaturnPath = new Text(topComp, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_textSaturnPath = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_textSaturnPath.verticalIndent = 10;
		textSaturnPath.setLayoutData(gd_textSaturnPath);

		final Label label_4 = new Label(topComp, SWT.NONE);
		final GridData gd_label_4 = new GridData();
		gd_label_4.verticalIndent = 10;
		label_4.setLayoutData(gd_label_4);
		label_4.setText(Messages.getString("ServerPreferencePage.6")); //$NON-NLS-1$

		portTxt = new Text(topComp, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_portTxt = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		portTxt.setLayoutData(gd_portTxt);
		new Label(topComp, SWT.NONE);

		final Composite composite = new Composite(topComp, SWT.NONE);
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, false,
				false);
		gd_composite.verticalIndent = 10;
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 3;
		composite.setLayout(gridLayout_1);

		final Button buttonAdd = new Button(composite, SWT.NONE);
		buttonAdd.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				NewSaturnServerDialog server = new NewSaturnServerDialog(Display
						.getDefault().getShells()[0], saturnServers);

				if (Window.OK == server.open()) {
					comboName.setItems(saturnServers.keySet().toArray(
							new String[0]));

					comboName.setText(server.getSaturnServerName());
				}
			}
		});
		final GridData gd_buttonAdd = new GridData();
		buttonAdd.setLayoutData(gd_buttonAdd);
		buttonAdd.setText(Messages.getString("ServerPreferencePage.New")); //$NON-NLS-1$

		final Button buttonModify = new Button(composite, SWT.NONE);
		buttonModify.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				EditSaturnServerDialog dialog = new EditSaturnServerDialog(
						Display.getDefault().getShells()[0], comboName
								.getText(), saturnServers);
				if (Window.OK == dialog.open()) {
					refreshTextValue(comboName.getText().trim());
					comboName.setText(dialog.getSaturnServerName());
				}
			}
		});
		final GridData gd_buttonModify = new GridData();
		gd_buttonModify.horizontalIndent = 10;
		buttonModify.setLayoutData(gd_buttonModify);
		buttonModify.setText(Messages.getString("ServerPreferencePage.Modify")); //$NON-NLS-1$

		final Button buttonDel = new Button(composite, SWT.NONE);
		buttonDel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				saturnServers.remove(comboName.getText());
				comboName
						.setItems(saturnServers.keySet().toArray(new String[0]));

				if (comboName.getItems().length != 0) {
					comboName.select(0);
					refreshTextValue(comboName.getText().trim());
				} else {
					comboName.setText(""); //$NON-NLS-1$
					textSaturnPath.setText(""); //$NON-NLS-1$
					textServer.setText(""); //$NON-NLS-1$
					textServerPath.setText(""); //$NON-NLS-1$
					portTxt.setText("");
				}
			}
		});
		final GridData gd_buttonDel = new GridData();
		gd_buttonDel.horizontalIndent = 10;
		buttonDel.setLayoutData(gd_buttonDel);
		buttonDel.setText(Messages.getString("ServerPreferencePage.Delete")); //$NON-NLS-1$

		IPreferenceStore preferenceStore = getPreferenceStore();

		Set<String> set = saturnServers.keySet();
		comboName.setItems(set.toArray(new String[0]));
		comboName.select(0);
		String saturnName = preferenceStore.getString(SATURN_NAME);
		if (saturnName == null || saturnName.equals("")) { //$NON-NLS-1$
			comboName.setText(preferenceStore.getString(SATURN_NAME));
		} else {
			comboName.setText(saturnName);
		}

		return topComp;
	}

	private void refreshTextValue(String name) {
		SaturnServer server = saturnServers.get(name);

		if (server != null) {
			textSaturnPath.setText(server.getDepolyPath());
			textServer.setText(server.getClassType());
			textServerPath.setText(server.getServerPath());
			portTxt.setText(server.getPort());
		}
	}

	@Override
	protected void performApply() {
		doSave();
	}

	@Override
	public boolean performOk() {
		doSave();
		return true;
	}

	private void doSave() {
		String type = comboName.getText();
		SaturnServerModel.saveSaturnServers(this.saturnServers);
		addPortToServerXml();
		if (type != null && !"".equals(type)) { //$NON-NLS-1$
			IPreferenceStore preferenceStore = getPreferenceStore();
			preferenceStore.setValue(SATURN_NAME, comboName.getText());
		}
	}
	
	public  void addPortToServerXml() {
		String path = textServerPath.getText();
		String serverPortPath = path +  "/conf/server.xml";
		serverPortPath = serverPortPath.replace("//", File.separator);
		File file = new File(serverPortPath);
		if(file.exists()) {
			file.delete();
			Map<String, String> portValue = new HashMap<String, String>(); 
			portValue.put( "port" , portTxt.getText());
			
			String tomcatServerTemplateUrl  = "/template/tomcatServerXml.ftl";
			
			FreemarkerUtils.writeTemplateToFile(tomcatServerTemplateUrl, portValue, serverPortPath);
		}
		
	}
}
